﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class ReporteDeUsuarios : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["cs"] = ConfigurationManager.ConnectionStrings["RFID"].ConnectionString;
    }
    protected void TxtCedula_TextChanged(object sender, EventArgs e)
    {

    }
    protected void BttnBuscar_Click(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = null;
        FechaTexBox.Text = null;
        FechaFinal.Text = null;
    }

    protected void odsRegistrosDeUsuario_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        LabelEntrada.Text = e.OutputParameters["mensaje"].ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        ExportToExcel("Informe.xls", GridView1);
    }

    private void ExportToExcel(string nameReport, GridView e)
    {
        HttpResponse response = Response;
        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        Page pageToRender = new Page();

        HtmlForm form = new HtmlForm();

        e.AllowPaging = false;
        e.DataBind();

        form.Controls.Add(e);

        pageToRender.Controls.Add(form);

        response.Clear();

        response.Buffer = true;

        response.ContentType = "application/vnd.ms-excel";

        response.AddHeader("Content-Disposition", "attachment;filename=" + nameReport);

        response.Charset = "UTF-8";

        response.ContentEncoding = Encoding.Default;

        pageToRender.RenderControl(htw);

        response.Write(sw.ToString());

        response.End();

    }
}
